package com.abel.quiz.services.impl;

import com.abel.quiz.dao.QuestionPoDao;
import com.abel.quiz.dao.BaseDao;
import com.abel.quiz.po.OptionsPo;
import com.abel.quiz.po.OptionsPoCriteria;
import com.abel.quiz.po.QuestionPo;
import com.abel.quiz.po.QuestionPoCriteria;
import com.abel.quiz.services.OptionsService;
import com.abel.quiz.services.QuestionService;
import com.abel.quiz.vo.OptionsDomain;
import com.abel.quiz.vo.QuestionDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author abel.lin
* @date_time 2017-08-08 10:02
* @description
*/
@Service
public class QuestionServiceImpl
    extends BaseServiceImpl<BaseDao<QuestionPo,QuestionPoCriteria>,QuestionPo,QuestionPoCriteria>
    implements QuestionService {

    @Autowired
    private QuestionPoDao questionPoDao;
    @Autowired
    private OptionsService optionsService;

    public QuestionServiceImpl(){
        super.baseMapper = questionPoDao;
    }

    public QuestionDomain selectDomainById(String id){
        QuestionPo po = questionPoDao.selectByPrimaryKey(id);
        QuestionDomain domain = QuestionDomain.genDomain(po);
        OptionsPoCriteria query = new OptionsPoCriteria();
        query.createCriteria().andQuestionIdEqualTo(id);
        List<OptionsPo> list = optionsService.selectByExample(query);
        domain.setOptionsList(list);
        return domain;
    }

    @Transactional
    public int insertSelective(QuestionDomain domain){
        int result = 0;
        result = questionPoDao.insertSelective(domain);

        if(domain.getOptionsList() != null && domain.getOptionsList().size() > 0){
            for(OptionsPo options : domain.getOptionsList()){
                options.setQuestionId(domain.getId());
                options.setCreateUser(domain.getCreateUser());
                options.setCreateTime(domain.getCreateTime());
                if(options.getCorrect() == null){
                    options.setCorrect(0);
                }
                optionsService.insertSelective(options);
            }
        }
        return result;
    }

    @Transactional
    public int updateByPrimaryKeySelective(QuestionDomain domain){
        int result = 0;
        result = questionPoDao.updateByPrimaryKeySelective(domain);

        OptionsPoCriteria query = new OptionsPoCriteria();
        query.createCriteria().andQuestionIdEqualTo(domain.getId());
        optionsService.deleteByExample(query);

        if(domain.getOptionsList() != null && domain.getOptionsList().size() > 0){
            for(OptionsPo options : domain.getOptionsList()){
                options.setQuestionId(domain.getId());
                options.setCreateUser(domain.getUpdateUser());
                options.setCreateTime(domain.getUpdateTime());
                if(options.getCorrect() == null){
                    options.setCorrect(0);
                }
                optionsService.insertSelective(options);
            }
        }
        return result;
    }

}
